//
//  RequestViewController.m
//  MApiSample
//
//  Created by IOS_HMX on 17/5/11.
//  Copyright © 2017年 Mitake. All rights reserved.
//

#import "RequestViewController.h"
#import "ResponseViewController.h"
@implementation RequestViewController
-(void)viewDidLoad
{
    [super viewDidLoad];
//    self.tableView.hidden = YES;
    [self registerServer];
    
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}
- (void)registerServer {

    [MApi setDebugMode:@{@"token":@"|F)w*7sYaoIdM+!#8d10LsT#0x|cA.xX/a@;a~",
                         @"enabled":@YES,
                         @"mode":@7}];
    
//    NSDictionary *options =
//                @{@"MAPI_DEBUG_AUTH_SERVERS":@[@"http://114.80.155.58:22016"],
//                  @"MAPI_DEBUG_QUOTE_SERVERS":@{
//                        @"pb":@[@{@"ip":@"http://114.80.155.58:22016"},@{@"ip":@"tcp://114.80.155.134:22017"}],
//
//                        @"sh":@[@{@"ip":@"http://114.80.155.61:22016"},@{@"ip":@"tcp://114.80.155.61:22017"}],
//                        @"shl2":@[@{@"ip":@"http://114.80.155.62:22016"},@{@"ip":@"tcp://114.80.155.62:22017"}],
//
//                        @"sz":@[@{@"ip":@"http://114.80.155.134:22016"},@{@"ip":@"tcp://114.80.155.58:22017"}],
//                        @"szl2":@[@{@"ip":@"http://114.80.155.57:22016"},@{@"ip":@"tcp://114.80.155.57:22017"}],
//
//                        @"hk10":@[@{@"ip":@"http://114.80.155.133:22016"},@{@"ip":@"tcp://114.80.155.133:22017"}],
//                        @"hk5":@[@{@"ip":@"http://114.80.155.133:22016"}], //港股通五档
//                        @"hka1":@[@{@"ip":@"http://114.80.155.133:22016"}],//实时一档
//                        @"hkd1":@[@{@"ip":@"http://114.80.155.139:22016"}], //延时一档
//                        @"hk1":@[@{@"ip":@"http://114.80.155.58:22016"}], //港股通一档
//
//                        @"hkaz":@[@{@"ip":@"http://114.80.155.133:22016"}],
//                        @"hkdz":@[@{@"ip":@"http://114.80.155.139:22016"}],
//                        @"cff":@[@{@"ip":@"http://114.80.155.61:22016"},@{@"ip":@"tcp://114.80.155.61:22017"}],// 中金所
//                        @"cf":@[@{@"ip":@"http://180.163.112.216:22016"},@{@"ip":@"tcp://180.163.112.216:22017"}],//大商所&郑商所&上期所&上期所原油
//                        @"gf":@[@{@"ip":@"http://114.80.155.134:22016"},@{@"ip":@"tcp://114.80.155.134:22017"}],//全球指数&外汇
//                        @"fe":@[@{@"ip":@"http://114.80.155.134:22016"},@{@"ip":@"tcp://114.80.155.134:22017"}],//外汇
//                        @"bj":@[@{@"ip":@"http://114.80.155.61:22016"},@{@"ip":@"tcp://114.80.155.61:22017"}],//新三板
//                        @"csi":@[@{@"ip":@"http://114.80.155.61:22016"},@{@"ip":@"tcp://114.80.155.61:22017"}],
////                        @"nf":@[@{@"ip":@"http://114.80.155.134:22013"}]//F10
//                        @"nf":@[@{@"ip":@"http://180.163.112.216:22013"}]//F10
//                          }
//                      };
    
//    NSDictionary *options =
//                   @{@"MAPI_DEBUG_AUTH_SERVERS":@[@"http://114.80.155.61:22016"],
//                     @"MAPI_DEBUG_QUOTE_SERVERS":@{
//                           @"pb":@[@{@"ip":@"http://114.80.155.61:22016"}],
//                           @"csi":@[@{@"ip":@"http://114.80.155.61:22016"},@{@"ip":@"tcp://114.80.155.61:22017"}],
//                           @"sh":@[@{@"ip":@"http://114.80.155.61:22016"},@{@"ip":@"tcp://114.80.155.61:22017"}],
//                           @"shl2":@[@{@"ip":@"http://114.80.155.50:22016"},@{@"ip":@"tcp://114.80.155.50:22017"}],          @"csi":@[@{@"ip":@"http://114.80.155.61:22016"},@{@"ip":@"tcp://114.80.155.61:22017"}],
//
//                           @"sz":@[@{@"ip":@"http://114.80.155.61:22016"},@{@"ip":@"tcp://114.80.155.61:22017"}],
//                           @"bj":@[@{@"ip":@"http://114.80.155.61:22016"},@{@"ip":@"tcp://114.80.155.61:22017"}],
//                           @"szl2":@[@{@"ip":@"http://114.80.155.57:22016"},@{@"ip":@"tcp://114.80.155.57:22017"}],
//                           @"hk10":@[@{@"ip":@"http://114.80.155.58:8601"},@{@"ip":@"tcp://114.80.155.58:8602"}]
//
//                     } };
    
    NSDictionary *options =
                   @{@"MAPI_DEBUG_AUTH_SERVERS":@[@"http://114.80.155.61:22016"],
                     @"MAPI_DEBUG_QUOTE_SERVERS":@{@"pb":@[@{@"ip":@"http://114.80.155.134:22016"}], @"sh":@[@{@"ip":@"http://114.80.155.134:22016"},@{@"ip":@"tcp://114.80.155.134:22017"},@{@"ip":@"tcp://114.80.155.61:22017"},@{@"ip":@"tcp://114.80.155.58:22017"}], @"sz":@[@{@"ip":@"http://114.80.155.134:22016"},@{@"ip":@"tcp://114.80.155.134:22017"},@{@"ip":@"tcp://114.80.155.61:22017"},@{@"ip":@"tcp://114.80.155.58:22017"}], @"szl2":@[@{@"ip":@"http://114.80.155.47:22016"},@{@"ip":@"tcp://114.80.155.57:22017"},@{@"ip":@"tcp://114.80.155.57:22017"}], @"shl2":@[@{@"ip":@"http://114.80.155.62:22016"},@{@"ip":@"tcp://114.80.155.62:22017"},@{@"ip":@"tcp://114.80.155.50:22017"}], @"szplus":@[@{@"ip":@"http://114.80.155.48:22016"},@{@"ip":@"tcp://114.80.155.48:22017"},@{@"ip":@"tcp://114.80.155.51:22017"}], @"shplus":@[@{@"ip":@"http://114.80.155.49:22016"},@{@"ip":@"tcp://114.80.155.49:22017"},@{@"ip":@"tcp://114.80.155.51:22017"}], @"hk":@[@{@"ip":@"http://114.80.155.133:22016"},@{@"ip":@"tcp://114.80.155.133:22017"}], @"hk1":@[@{@"ip":@"http://58.63.252.56:22016"},@{@"ip":@"tcp://58.63.252.56:22017"}], @"hk5":@[@{@"ip":@"http://58.63.252.56:22016"},@{@"ip":@"tcp://58.63.252.56:22017"}], @"hk10":@[@{@"ip":@"114.80.155.133:22016"},@{@"ip":@"tcp://114.80.155.133:22017"}], @"hka1":@[@{@"ip":@"http://58.63.252.56:22016"},@{@"ip":@"tcp://58.63.252.56:22017"}], @"hkd1":@[@{@"ip":@"http://114.80.155.139:22016"},@{@"ip":@"tcp://114.80.155.139:22017"}], @"hkaz":@[@{@"ip":@"http://114.80.155.58:8601"},@{@"ip":@"tcp://114.80.155.58:8602"}], @"hkdz":@[@{@"ip":@"http://114.80.155.139:22016"},@{@"ip":@"tcp://114.80.155.139:22017"}], @"bj":@[@{@"ip":@"http://114.80.155.134:22016"},@{@"ip":@"tcp://114.80.155.134:22017"}], @"bz":@[@{@"ip":@"http://114.80.155.134:22016"},@{@"ip":@"tcp://114.80.155.134:22017"}],
                     } };

    [MApi registerAPP:@"VVW0Fno7BEZt1a/y6KLM36uj9qcjw7CAHDwWZKDlWDs="
          withOptions:options
    completionHandler:^(NSError *error) {
        if (!error) {
//            TOCK
            self.tableView.hidden = NO;
            NSLog(@"注册成功");
            NSLog(@"当前版本：%@",[MApi version]);
        }
        else {
            NSLog(@"注册失败, %@", error);
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Error" message:[error localizedDescription] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
            [alert show];
        }
    }];

    [MApi setSourcePermissions:@{
        MApiMarketSH:@(MApiSourceLevel1),
        MApiMarketSZ:@(MApiSourceLevel1),
        MApiMarketBJ:@(MApiSourceLevel1),
        MApiMarketHK:@(MApiSourceHK10),
        MApiMarketCFF:@(MApiSourceCFF1),
        MApiMarketDCE:@(MApiSourceDCE1),
        MApiMarketCZCE:@(MApiSourceCZCE1),
        MApiMarketSHFE:@(MApiSourceSHFE1),
        MApiMarketINE:@(MApiSourceINE1)
        }];
    
//    [self MSnapQuoteRequest];
}

- (void)MSnapQuoteRequest{
    {
        MSnapQuoteRequest *request = [[MSnapQuoteRequest alloc] init];
        request.code = @"H30040.csi";
        [MApi sendRequest:request completionHandler:^(MResponse *resp) {
            MSnapQuoteResponse *response = (MSnapQuoteResponse *)resp;
            if (response.status == MResponseStatusSuccess) {
                MStockItem *item = (MStockItem *)response.stockItem;
                NSLog(@"isChinaUser:%d",[MApi isChinaUser]);
            }
        }];
    }
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.infos.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.infos[section][@"infos"] count];
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return self.infos[section][@"title"];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"cellIdentifier";
    UITableViewCell * cell =[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    
    cell.textLabel.text = self.infos[indexPath.section][@"infos"][indexPath.row][@"title"];
    cell.detailTextLabel.text = self.infos[indexPath.section][@"infos"][indexPath.row][@"request"];
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *class = self.infos[indexPath.section][@"infos"][indexPath.row][@"class"];
    if (class) {
        UIViewController *vc = [[NSClassFromString(class) alloc]init];
        [self.navigationController pushViewController:vc animated:YES];
    }else {
        ResponseViewController *vc = [[ResponseViewController alloc]init];
        vc.info = self.infos[indexPath.section][@"infos"][indexPath.row];
        vc.title = self.infos[indexPath.section][@"infos"][indexPath.row][@"title"];
        [self.navigationController pushViewController:vc animated:YES];
    }
    
    return;
}
@end
